package com.tianbing.adlottery.controller.hordeadmin;

/**
 * Add one sentence class summary here.
 * Add class description here.
 *
 * @author conan
 * @version 1.0, 2005-12-6
 */

import org.apache.log4j.Logger;
import org.apache.oro.text.perl.Perl5Util;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.Site;
import com.tianbing.adlottery.util.ControllerFormBean;

/**
 * @author conan
 * 
 */
public class EditMyServedProductProfileValidator implements Validator {
	static Logger logger = Logger.getLogger(EditMyServedProductProfileValidator.class.getName());

	private static final String EMAIL_REGEXP = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";

	private static final String PHONE_REGEXP = "/(\\({0,1})(\\d{3})(\\){0,1})(\\s|-)*(\\d{3})(\\s|-)*(\\d{4})/";

	private ISiteDao siteDao;

	// IPartyService partyService;

	/**
	 * 
	 */
	public EditMyServedProductProfileValidator() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class clazz) {
		return clazz.equals(ControllerFormBean.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 *      org.springframework.validation.Errors)
	 */
	public void validate(Object command, Errors errors) {
		ControllerFormBean formbean = (ControllerFormBean) command;
		Site site = formbean.getSite();
		validateSiteId(site.getSysId(), site.getSiteId(), errors);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "site.name", "required.name", "Store name is mandatory");
	}

	private void validateSiteId(long siteSysId, String siteId, Errors errors) {
		ValidationUtils
				.rejectIfEmptyOrWhitespace(errors, "site.siteId", "required.siteId", "Domain name is mandatory!");
		Site site = siteDao.findSiteBySiteId(siteId);
		if (site != null && site.getSysId().compareTo(siteSysId) != 0) {
			errors.rejectValue("site.siteId", "Domain name already exist!", null, "域名已存�?!");
		}
	}

	private void validatePassword(String password, String confirmPassword, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "password", "required.password", "Password is required");
		if (!password.equals(confirmPassword)) {
			errors.rejectValue("password", "Password is not match ConfirmPassword!", null, "瀵嗙爜涓庣�?�璁ゅ瘑鐮佷笉绗︼紒");
		} else {
			logger.info("validatePassword Passed.");
		}
	}

	private void validatePhone(String phone, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "required.phone", "Phone is required");
		Perl5Util perl5Util = new Perl5Util();
		if (!perl5Util.match(PHONE_REGEXP, phone)) {
			errors.reject("invalid.phone", "Phone number is invalid");
		}
	}

	private void validateEmail(String email, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "party.partyId", "required.email_addr", "E-mail is required");
		Perl5Util perl5Util = new Perl5Util();
		if (!perl5Util.match(EMAIL_REGEXP, email)) {
			logger.info("validateEmail email error,failed.");
			errors.rejectValue("party.partyId", "Email Formart Error!", null, "鐢甸偖鏍煎紡涓嶆纭紒");
		} else {
			// ModelRequest request = new ModelRequest();
			// Party party =
			// partyService.getPartyDetail(ModelRequest.newRequest().setPartyId(email)).getParty();
			// if (party != null) {
			// logger.info("validateEmail email exists,failed.");
			// errors.rejectValue("party.partyId", "Email has already exists!",
			// null, "鐢甸偖宸茬粡瀛樺湪锛?");
			// } else {
			// logger.info("validateEmail email not exists, Passed.");
			// }
		}
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	// public IPartyService getPartyService() {
	// return partyService;
	// }
	//
	// public void setPartyService(IPartyService partyService) {
	// this.partyService = partyService;
	// }
}